#include "diaevents.h"
#include "ui_diaevents.h"
#include <QDebug>

DiaEvents::DiaEvents(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DiaEvents)
{

    ui->setupUi(this);
    this->ui->btn_ok->setEnabled(false);
    this->ui->dt_ev->setDateTime(QDateTime::currentDateTime());
}

DiaEvents::~DiaEvents()
{

    // убираем мусор за собой
    for(int i = this->ui->tw_tariffs->rowCount(); i >=0 ; i--){
        QTableWidgetItem * wit;
        wit = this->ui->tw_tariffs->takeItem(i,2);
        delete wit;
        wit = this->ui->tw_tariffs->takeItem(i,1);
        delete wit;
        wit = this->ui->tw_tariffs->takeItem(i,0);
        delete wit;
    }

    delete ui;

}

void DiaEvents::setEvCapacity(int cap){
    this->ui->sb_cap->setValue(cap);
}

int DiaEvents::getEvCapacity(){
    return this->ui->sb_cap->value();
}

void DiaEvents::setEvDesc(const QString &desc){
    this->ui->te_desc->setText(desc);
}

QString DiaEvents::getEvDesc(){
    return this->ui->te_desc->document()->toPlainText();
}

void DiaEvents::setEvDt(QDateTime dt){
    this->ui->dt_ev->setDateTime(dt);
}

QDateTime DiaEvents::getEvDt(){
    return this->ui->dt_ev->dateTime();
}

void DiaEvents::setEvName(const QString &name){
    this->ui->le_name->setText(name);
}

QString DiaEvents::getEvName(){
    return this->ui->le_name->text();
}

void DiaEvents::setEvStatus(int status){
    this->ui->cb_status->setCurrentIndex(status);
}

int DiaEvents::getEvStatus(){
    return this->ui->cb_status->currentIndex();
}

void DiaEvents::slotValidateName(){
    // проверяем имя
    if(this->ui->le_name->text().size() != 0){
        this->ui->btn_ok->setEnabled(true);
    }
}

void DiaEvents::setTariffsList(QStringList a){
    this->ui->tw_tariffs->setRowCount(a.size()/2);
    int ind = 0 ;
    for(int i = 0 ; i < a.size()/2 ; i++){


    QTableWidgetItem * wit = new QTableWidgetItem;
    wit->setText(a.at(ind));
    this->ui->tw_tariffs->setItem(i,0,wit);


    wit = new QTableWidgetItem;
    wit->setText(a.at(1+ind));
    this->ui->tw_tariffs->setItem(i,1,wit);


    wit = new QTableWidgetItem;
    wit->setFlags(Qt::ItemIsUserCheckable | wit->flags());
    wit->setCheckState(Qt::Unchecked);
    this->ui->tw_tariffs->setItem(i,2,wit);


    ind = ind +2;
    }
    this->ui->tw_tariffs->resizeColumnsToContents();
    this->ui->tw_tariffs->horizontalHeader()->setStretchLastSection(true);

}

void DiaEvents::setTariffsListEnabled(QStringList a){
    for(int i = 0 ; i < a.size() ; i++){
        for(int j = 0; j < this->ui->tw_tariffs->rowCount(); j++){
        if(a.at(i) == this->ui->tw_tariffs->item(j,0)->text()){
            this->ui->tw_tariffs->item(j,2)->setCheckState(Qt::Checked);
        }}
    }



}

QString DiaEvents::getTariffsListEnabled(){
    QString ret;
    bool bb = true;
    for(int i = 0 ; i < this->ui->tw_tariffs->rowCount() ; i++){
        if(this->ui->tw_tariffs->item(i,2)->checkState() == Qt::Checked){
            if(!bb){
                ret.append(",");

            }
            ret.append(this->ui->tw_tariffs->item(i,0)->text());
            bb=false;
        }
    }
    return ret;
}
